/*
 * File Name:               XJsonUtility.cs
 * 
 * Description:             用来替代Unity的 JsonUtility 
 *                          1. 避免7层最大限制
 *                          2. 更高的扩展性
 * Author:                  lisiyu <576603306@qq.com>

 * Create Date:             2017/06/12
 */

using Newtonsoft.Json;
public class XJsonUtility
{
    private static JsonSerializerSettings mSettings_Serilize;
    private static JsonSerializerSettings mSettings_Desrialize;
    static XJsonUtility()
    {
        mSettings_Serilize = new JsonSerializerSettings() {
            ContractResolver = new XJsonNoPropertyResolver(),               // 不序列化属性
        };

        mSettings_Desrialize = new JsonSerializerSettings() {
            ObjectCreationHandling = ObjectCreationHandling.Replace,        // 初始化时 使用序列化值替换默认值
        };
    }

    public static T FromJson<T>(string json)
    {
        return JsonConvert.DeserializeObject<T>(json, mSettings_Desrialize);
    }

    public static string ToJson(object obj)
    {
        return JsonConvert.SerializeObject(obj, Formatting.None, mSettings_Serilize);
    }

    public static string ToJson_Pretty(object obj)
    {
        return JsonConvert.SerializeObject(obj, Formatting.Indented, mSettings_Serilize);
    }
}
